﻿using Expect.Core;
using Expect.Core.Helpers;
using Expect.Core.Interfaces;
using WatiN.Core;

namespace Expect.WatiN
{
    using System;

    /// <summary>
    /// Base class that is both a WatiN's Page and Expect's fixture 
    /// </summary>
    public class ExpectPage : Page, IActorFixtureFormat
    {
        /// <summary>
        /// Fill arguments and definition if necessary
        /// </summary>
        /// <returns></returns>
        public virtual void Resolve()
        {
            this.InitializeContents(Context.Browser());
        }

        /// <summary>
        /// Execute the fixture for the provided row
        /// </summary>
        public virtual bool? RunRow()
        {
            return Context.RunAsFluentFixture(this.Fixture, this.Row, this);
        }

        /// <summary>
        /// Called when there is no more rows to process
        /// </summary>
        public virtual void EndRun()
        {
        }

        private IExecutionContext _context;

        /// <summary>
        /// Execution context
        /// </summary>
        public virtual IExecutionContext Context
        {
            get
            {
                if (_context == null && Specification != null)
                {
                    return Specification.Context;
                }
                return this._context;
            }
            set
            {
                this._context = value;
            }
        }

        /// <summary>
        /// Current specification
        /// </summary>
        public virtual SpecificationDefinition Specification { get; set; }

        /// <summary>
        /// Fixture definition
        /// </summary>
        public virtual FixtureDefinition Fixture { get; set; }

        /// <summary>
        /// Current row
        /// </summary>
        public virtual FixtureRow Row { get; set; }
    }
}
